#!/bin/bash

##  init and syncronization script for GAP 4.5 under Linux
##  (C) Frank Lübeck
arch=`uname -m`
if [ ! -f gapsync.issyncdir ]; then
  echo Welcome to the GAP installation by syncronizing a reference installation.
  echo 
  echo "Remarks and suggestions to: Frank.Luebeck@Math.RWTH-Aachen.De"
  echo
  echo "You need: A Linux system"
  a=`uname -s`
  if [ $a'X' == LinuxX ]; then
    echo "       ... looks ok"
  else
    echo "       ... hm, found: "$a
  fi

  echo "          on i686 and x86_64 compatible architecture"
  if [ $arch'X' == i686X ]; then
    echo "       ... looks ok (i686)"
  else
    if [ $arch'X' == x86_64X ]; then
      echo "       ... looks ok (x86_64)"
    else
      echo "       ... hm, found: "$a
    fi
  fi

  echo "          the 'rsync' utility program"
  rsync=`which rsync`
  if [ $rsync'X' != X ]; then
    echo "       ... found: "$rsync
  else
    echo "       ... hm, did not find 'rsync', please install it and start me again"
    exit 1
  fi

  echo "          a directory with at least 1.4 GB of free space"
  echo
  echo

  echo "Please, tell me an absolute path for the installation of GAP "
  echo "(default is /usr/local/lib).  Everything will be stored in a "
  echo "subdirectory 'gap4r5', except for the start scripts."
  read -p '---> ' -e gappath
  if [ $gappath'X' == 'X' ]; then
    gappath="/usr/local/lib/gap4r5"
  else
    gappath=$gappath'/gap4r5'
  fi
  # avoid multiple //
  gappath=${gappath//\/\//\/}

  echo "Also, tell me an absolute path for the scripts to start GAP "
  echo "(default is /usr/local/bin)"
  echo ""
  read -p '---> ' -e binpath
  if [ $binpath'X' == 'X' ]; then
    binpath="/usr/local/bin"
  fi
  
  # check if some scripts to install would overwrite something
  exist=""
  for nam in gap xgap gapL gapsync pargap gac; do
    if [ -f $binpath/$nam ]; then
      exist=$exist" "$nam
    fi
  done
  if [ $arch'X' == x86_64X ]; then
    for nam in gap64 gap64L pargap64 gac64; do
      if [ -f $binpath/$nam ]; then
        exist=$exist" "$nam
      fi
    done
  fi
  if [ "$exist"'X' != 'X' ]; then
    echo ""
    echo "There are already the following scripts in "$binpath":"
    echo "                "$exist
    echo "If you don't want me to delete them now, say 'No' here and move"
    echo "them away before starting me again."
    echo "Shall I delete "$exist
    echo "            in "$binpath" now? (y/N)"
    read -n 1 del
    echo
    if [ $del'X' != 'yX' ]; then
      echo Ok, start me again after moving them away.
      exit 3
    else
      rm -f $binpath/{gap,xgap,gapL,gapsync,pargap,gac,gap64,gap64L,pargap64,gac64}
    fi
  fi
  
  echo "Going to syncronize GAP in: "$gappath

  echo "Shall I start to copy GAP to your machine? (y/N)"
  read -n 1 start
  echo
  if [ $start'X' != 'yX' ]; then
    echo Ok, bye.
    exit 2
  fi

  echo 'Creating directory: '$gappath
  if ! mkdir -p $gappath; then
    echo 'Cannot create directory '$gappath'   --- bye.'
    exit 3
  fi  
  if ! cd $gappath; then
    echo 'Cannot change to directory '$gappath'   --- bye.'
    exit 4
  fi

  # adding a directory for customization
  mkdir -p local/bin
  mkdir -p local/pkg
  mkdir -p bin

  echo $binpath > local/bin/binpath
else
  rsync=`which rsync`
  gappath=`pwd`/..
  binpath=`cat ../local/bin/binpath`
  cd $gappath
fi

line=''$rsync' -avz --delete --port 8731 --exclude=local/ rsync.gap-system.org::gap4r5/ .'
echo 'Calling: '$line 

if ! $line; then
  echo 'Synchronization failed --- bye.'
  exit 5
fi

# make sure to have the 'local' directory
mkdir -p local/bin
mkdir -p local/pkg

echo "Creating new workspace."
if [ -f local/bin/CreateWorkspace ]; then
  echo "Using local/bin/CreateWorkspace ..."
  local/bin/CreateWorkspace
else
  echo "Using gapsync/CreateWorkspace.sh ..."
  echo "      (You can customize this by copying gapsync/CreateWorkspace.sh to"
  echo "       local/bin/CreateWorkspace and editing this copy.)"
  gapsync/CreateWorkspace.sh
fi
if [ $arch'X' = x86_64X ]; then
  echo "Also creating new workspace for 64-bit version ..."
  if [ -f local/bin/CreateWorkspace64 ]; then
    echo "Using local/bin/CreateWorkspace64 ..."
    local/bin/CreateWorkspace64
  elif [ -f local/bin/CreateWorkspace ]; then
    echo "Using local/bin/CreateWorkspace ..."
    local/bin/CreateWorkspace
  else
    echo "Using gapsync/CreateWorkspace.sh x86_64 ..."
    echo "      (You can customize this by copying gapsync/CreateWorkspace.sh to"
    echo "       local/bin/CreateWorkspace64 or local/bin/CreateWorkspace"
    echo "       and editing this copy.)"
    gapsync/CreateWorkspace.sh x86_64
  fi
fi
  
mkdir -p $binpath
if [ ! -f $binpath/gap ]; then
  rm -f local/bin/gap
  echo "Creating start script "$binpath"/gap," 
  echo "      (you can customize this by editing local/bin/gap)."
  echo "#!/bin/sh" > local/bin/gap
  echo "GAP_DIR="$gappath >> local/bin/gap
  cat gapsync/gap.sh >> local/bin/gap
  chmod 755 local/bin/gap
  ln -s $gappath/local/bin/gap $binpath
fi
if [ ! -f $binpath/xgap ]; then
  rm -f local/bin/xgap
  echo "Creating start script "$binpath"/xgap," 
  echo "      (you can customize this by editing local/bin/xgap)."
  echo "#!/bin/sh" > local/bin/xgap
  echo "GAP_DIR="$gappath >> local/bin/xgap
  cat gapsync/xgap.sh >> local/bin/xgap
  chmod 755 local/bin/xgap
  ln -s $gappath/local/bin/xgap $binpath
fi
if [ ! -f $binpath/pargap ]; then
  rm -f local/bin/pargap
  echo "Creating start script "$binpath"/pargap," 
  echo "      (you can customize this by editing local/bin/pargap)."
  echo "#!/bin/sh" > local/bin/pargap
  echo "GAP_DIR="$gappath >> local/bin/pargap
  cat gapsync/pargap.sh >> local/bin/pargap
  chmod 755 local/bin/pargap
  ln -s $gappath/local/bin/pargap $binpath
fi
if [ ! -f $binpath/gapL ]; then
  rm -f local/bin/gapL
  echo "Creating start script "$binpath"/gapL," 
  echo "      (you can customize this by editing local/bin/gapL)."
  echo "#!/bin/sh" > local/bin/gapL
  echo "GAP_DIR="$gappath >> local/bin/gapL
  echo "BINPATH="$binpath >> local/bin/gapL
  cat gapsync/gapL >> local/bin/gapL
  chmod 755 local/bin/gapL
  ln -s $gappath/local/bin/gapL $binpath
fi
if [ ! -f $binpath/gapsync ]; then
  rm -f local/bin/gapsync
  echo "Adjusting update script "$binpath"/gapsync," 
  echo "#!/bin/sh" > local/bin/gapsync
  echo "cd "$gappath"/gapsync" >> local/bin/gapsync
  cat gapsync/gapsync >> local/bin/gapsync
  chmod 755 local/bin/gapsync
  ln -s $gappath/local/bin/gapsync $binpath
fi
if [ ! -f $binpath/gap ]; then
  echo "Error: Could not create link "$binpath"/gap"
  exit 6
fi
if [ $arch'X' = x86_64X ]; then
  if [ ! -f $binpath/gap64 ]; then
    rm -f local/bin/gap64
    echo "Creating start script "$binpath"/gap64," 
    echo "      (you can customize this by editing local/bin/gap)."
    echo "#!/bin/sh" > local/bin/gap64
    echo "GAP_DIR="$gappath >> local/bin/gap64
    cat gapsync/gap64.sh >> local/bin/gap64
    chmod 755 local/bin/gap64
    ln -s $gappath/local/bin/gap64 $binpath
  fi
  if [ ! -f $binpath/pargap64 ]; then
    rm -f local/bin/pargap64
    echo "Creating start script "$binpath"/pargap64," 
    echo "      (you can customize this by editing local/bin/pargap64)."
    echo "#!/bin/sh" > local/bin/pargap64
    echo "GAP_DIR="$gappath >> local/bin/pargap64
    cat gapsync/pargap64.sh >> local/bin/pargap64
    chmod 755 local/bin/pargap64
    ln -s $gappath/local/bin/pargap64 $binpath
  fi
  if [ ! -f $binpath/gap64L ]; then
    rm -f local/bin/gap64L
    echo "Creating start script "$binpath"/gap64L," 
    echo "      (you can customize this by editing local/bin/gapL)."
    echo "#!/bin/sh" > local/bin/gap64L
    echo "GAP_DIR="$gappath >> local/bin/gap64L
    echo "BINPATH="$binpath >> local/bin/gap64L
    cat gapsync/gap64L >> local/bin/gap64L
    chmod 755 local/bin/gap64L
    ln -s $gappath/local/bin/gap64L $binpath
  fi
fi

